package com.sylli.oeuf.server.db.sql;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;

import com.sylli.oeuf.server.exception.BasedException;
import com.sylli.oeuf.server.exception.ExceptionCause;
import com.sylli.oeuf.server.object.transferable.BasedTO;
import com.sylli.oeuf.server.object.transferable.MonsterTO;
import com.sylli.oeuf.server.object.transferable.StatTO;
import com.sylli.oeuf.server.object.transferable.StrategyBundleTO;

public class MySQLMonster extends MySQLBasedProvider<MonsterTO> {

	public MySQLMonster() {
		interactTableName = "monster";
	}
	
	@Override
	protected PreparedStatement convertObjectToInsertQuery(MonsterTO obj)
			throws BasedException {
		try {
			PreparedStatement ps = mysqlConnector.getConnectionObject().prepareStatement(
					"INSERT INTO " + interactTableName 
					+ " (name, stat_hp, stat_mp, stat, level, exp, flag, strategy)"
					+ " VALUES (?,?,?,?,?,?,?,?)", 
					Statement.RETURN_GENERATED_KEYS);
			ps.setString(1, obj.getName());
			ps.setInt(2, obj.getStatHp());
			ps.setInt(3, obj.getStatMp());
			ps.setBytes(4, obj.getStat().toBytes());
			ps.setInt(5, obj.getLevel());
			ps.setInt(6, obj.getExp());
			ps.setInt(7, obj.getFlag());
			ps.setBytes(8, obj.getStrategyBundle().toBytes());
			return ps;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryCreationFail, e);
		} catch (IOException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryCreationFail, e);
		}
	}

	@Override
	protected PreparedStatement convertObjectToUpdateQuery(MonsterTO obj)
			throws BasedException {
		try {
			PreparedStatement ps = mysqlConnector.getConnectionObject().prepareStatement(
					"UPDATE " + interactTableName 
					+ " SET name=?, stat=?, stat_hp=?, stat_mp=?, level=?, exp=?, flag=?, strategy=? WHERE monster_id = ?;");
			ps.setString(1, obj.getName());
			ps.setBytes(2, obj.getStat().toBytes());
			ps.setInt(3, obj.getStatHp());
			ps.setInt(4, obj.getStatMp());
			ps.setInt(5, obj.getLevel());
			ps.setInt(6, obj.getExp());
			ps.setInt(7, obj.getFlag());
			ps.setBytes(8, obj.getStrategyBundle().toBytes());
			ps.setInt(9, obj.getMonsterId());
			return ps;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryUpdateFail, e);
		} catch (IOException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryUpdateFail, e);
		}
	}

	@Override
	protected MonsterTO convertResultSetToObject(ResultSet rs)
			throws BasedException {
		try {
			MonsterTO sto = new MonsterTO();
			rs.first();
			sto.setMonsterId(rs.getInt(1));
			sto.setName(rs.getString(2));
			sto.setStatHp(rs.getInt(3));
			sto.setStatMp(rs.getInt(4));
			sto.setStat((StatTO) BasedTO.fromBytes(rs.getBytes(5)));
			sto.setLevel(rs.getInt(6));
			sto.setExp(rs.getInt(7));
			sto.setFlag(rs.getInt(8));
			sto.setStrategyBundle((StrategyBundleTO) BasedTO.fromBytes(rs.getBytes(9)));
			return sto;
		} catch (SQLException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryAccessingFail, e);
		} catch (IOException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryAccessingFail, e);
		} catch (ClassNotFoundException e) {
			throw new BasedException(ExceptionCause.DatabaseEntryAccessingFail, e);
		}
	}

	@Override
	protected Collection<MonsterTO> convertResultSetToObjects(ResultSet rs)
			throws BasedException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected PreparedStatement convertObjectToInsertQuery(
			Collection<MonsterTO> objs) throws BasedException {
		// TODO Auto-generated method stub
		return null;
	}

}
